package com.ysy.tz.mfg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.DefaultKeyValuePair;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.dao.ArriveFactoryDao;
import com.ysy.tz.mfg.dao.DataListDao;
import com.ysy.tz.mfg.dao.InspectionWeightDao;
import com.ysy.tz.mfg.dto.InspectionWeightDto;
import com.ysy.tz.mfg.entity.ArriveFactoryEntity;
import com.ysy.tz.mfg.entity.InspectionWeightEntity;
import com.ysy.tz.mfg.entity.UserEntity;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;

public class InspectionWeightService {
    public InspectionWeightService() {
    }

    public ArrayList<DefaultKeyValuePair> searchPigType() {
        return (ArrayList)SQLUtils.executeSQLWithReturn(DataListDao.class, (dataListDao) -> {
            try {
                UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
                DefaultKeyValuePair condition = new DefaultKeyValuePair();
                condition.setEnterpriseId(currentUser.getEnterpriseId());
                ArrayList<DefaultKeyValuePair> pigTypeList = dataListDao.searchPigTypeList(condition);
                return pigTypeList;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        });
    }

    public ArrayList<InspectionWeightEntity> searchInspectionWeightSum(InspectionWeightDto condition) {
        return (ArrayList)SQLUtils.executeSQLWithReturn(InspectionWeightDao.class, (inspectionWeightDao) -> {
            try {
                UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
                condition.setEnterpriseId(currentUser.getEnterpriseId());
                ArrayList<InspectionWeightEntity> inspectionWeightEntities = inspectionWeightDao.sumForPage(condition);
                return inspectionWeightEntities;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        });
    }

    public PageInfo<InspectionWeightEntity> searchInspectionWeightList(InspectionWeightDto condition) {
        return (PageInfo)SQLUtils.executeSQLWithReturn(InspectionWeightDao.class, (inspectionWeightDao) -> {
            UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
            condition.setEnterpriseId(currentUser.getEnterpriseId());
            PageHelper.startPage(condition.getPageNum(), condition.getPageSize());

            try {
                List inspectionWeightEntities = inspectionWeightDao.listForPage(condition);
                PageInfo<InspectionWeightEntity> result = new PageInfo(inspectionWeightEntities);
                return result;
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        });
    }

    public InspectionWeightEntity searchNoWeightPig(String inputPigShipNo) {
        ArrayList<InspectionWeightEntity> inspectionWeightEntities = (ArrayList)SQLUtils.executeSQLWithReturn(InspectionWeightDao.class, (inspectionWeightDao) -> {
            try {
                return inspectionWeightDao.searchNoWeightPigs(ApplicationContext.machineCode, (String)SessionUtils.getAttribute(SessionUtils.LOGIN_DAY), ApplicationContext.enterpriseId);
            } catch (Exception var2) {
                var2.printStackTrace();
                return null;
            }
        });
        if (CollectionUtils.isNotEmpty(inspectionWeightEntities)) {
            InspectionWeightEntity entity = (InspectionWeightEntity)inspectionWeightEntities.get(0);
            if ((Boolean)SQLUtils.executeSQLWithReturn(InspectionWeightDao.class, (inspectionWeightDao) -> {
                try {
                    return inspectionWeightDao.hasWeight(entity.getPigshipno(), (String)SessionUtils.getAttribute(SessionUtils.LOGIN_DAY), entity.getSeqNo(), ApplicationContext.enterpriseId);
                } catch (Exception var3) {
                    var3.printStackTrace();
                    return false;
                }
            })) {
                entity.setPigshipno(inputPigShipNo);
            } else {
                ArrayList<DefaultKeyValuePair> internalSupplys = (ArrayList)SQLUtils.executeSQLWithReturn(InspectionWeightDao.class, (inspectionWeightDao) -> {
                    return inspectionWeightDao.searchPigTypeWithShipNo(entity.getPigshipno(), ApplicationContext.enterpriseId);
                });
                if (CollectionUtils.isNotEmpty(internalSupplys)) {
                    entity.setInternalSupplyCode(((DefaultKeyValuePair)internalSupplys.get(0)).getKeyPair());
                    entity.setInternalSupplyName(((DefaultKeyValuePair)internalSupplys.get(0)).getValuePair());
                }
            }

            return entity;
        } else {
            return null;
        }
    }

    public ArrayList<DefaultKeyValuePair> searchExperimentTypeList() {
        return (ArrayList)SQLUtils.executeSQLWithReturn(DataListDao.class, (dataListDao) -> {
            return dataListDao.searchExperimentTypeList(ApplicationContext.enterpriseId);
        });
    }

    public Boolean checkRejection(InspectionWeightEntity inspectionWeightEntity) {
        return (Boolean)SQLUtils.executeSQLWithReturn(ArriveFactoryDao.class, (arriveFactoryDao) -> {
            try {
                return arriveFactoryDao.hasRejection(inspectionWeightEntity.getPigshipno(), ApplicationContext.enterpriseId);
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        });
    }

    public Integer slaughteredCount(InspectionWeightEntity inspectionWeightEntity) {
        return (Integer)SQLUtils.executeSQLWithReturn(ArriveFactoryDao.class, (arriveFactoryDao) -> {
            try {
                ArrayList<ArriveFactoryEntity> arriveFactoryEntities = arriveFactoryDao.searchSlaughterCount(inspectionWeightEntity.getPigshipno(), ApplicationContext.enterpriseId);
                if (CollectionUtils.isNotEmpty(arriveFactoryEntities)) {
                    ArriveFactoryEntity entity = (ArriveFactoryEntity)arriveFactoryEntities.get(0);
                    return Integer.parseInt(entity.getPigQty()) - Integer.parseInt(entity.getActiveQty());
                } else {
                    return null;
                }
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        });
    }

    public void changePigType(InspectionWeightEntity target) throws Exception {
        SQLUtils.executeSQLException(InspectionWeightDao.class, (inspectionWeightDao) -> {
            inspectionWeightDao.changePigType(target);
        });
    }

    public void takeToExperiment(InspectionWeightEntity target) throws Exception {
        SQLUtils.executeSQLException(InspectionWeightDao.class, (inspectionWeightDao) -> {
            inspectionWeightDao.takeToExperiment(target);
        });
    }
}
